<?php
//go ahead and get the blog object from the filesystem
require('Blog.inc');
$blog = new Blog(null);
//create the connection to the DB
$dbh = new PDO('mysql:host=localhost;dbname=blog', 'root', '');
//After they update a record, I want to redirect them to the list page.  This is how I will decide if I will redirect them or not.  It uses Javascript.
$redirect = false;

// If they are coming here on a get, then they came from the list page.  Get the blog they requested.
if($_GET && $_GET['id']>0){
	$blog = $blog->getOneBlog($dbh, $_GET['id']);
}

//If they are coming here with a post, then we assume they are editing the blog so we will update the blog.
if($_POST){
	$blog = new Blog($_POST);
	$blog->update($dbh);
	$redirect = true;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Bryant's Blog</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
		<script type="text/javascript" src="common.js"></script>
		<?php
		if($redirect){
		echo "<script type='text/javascript'> window.location='myBlog.php'; </script>";
		}
		?>
	
	</head>

	<body>
		<div id="container">

			<div id="header">
			</div>

			<div id="wrapper">
				<div id="main">
					
					<form method="post" action="editBlog.php">
						<input type="hidden" name="id" value='<?php echo "$blog->id";?>' />
						Title:  
						<input type="text" name="title" id="title" size="45" value='<?php echo "$blog->title"; ?>' /> <br />
						Blog:<br />
						<textarea name="body" id="body"><?php echo "$blog->body"; ?></textarea><br />
						<input type="submit" name="submit" value="submit" />
					</form>
				</div>
			</div>

			<div id="footer">
Bryant Larsen copyright 2008
			</div>
			
		</div>
	</body>

</html>
